﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Functions Exercise 5</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <h1>Function that counts how many times given number appears in given array.</h1>
    <button onclick="execute()">Execute</button>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>
        function countNumberInArray(num, arr)
        {
            var numCount = 0;

            for (var idx = 0; idx < arr.length; idx++)
            {
                if (arr[idx] == num)
                {
                    numCount++;
                }
            }

            return numCount;
        }

        function unitTest()
        {
            var testCaseInputNum = [2, 3, 4, 0];
            var testCaseInputArr = [[1, 2, 3, 4, 5], [4, 2, 3, 4, 5, 3], [1, 2], [0, 0, 0, 0]];
            var testCaseResult = [1, 2, 0, 4];

            for (var idx = 0; idx < testCaseResult.length; idx++)
            {
                var testRes = countNumberInArray(testCaseInputNum[idx], testCaseInputArr[idx]);
                jsConsole.writeLine("Count " + testCaseInputNum[idx] + " in " + testCaseInputArr[idx] + " = " + testRes);
                if (testCaseResult[idx] == testRes)
                {
                    jsConsole.writeLine("Test is OK");
                }
                else
                {
                    jsConsole.writeLine("Test is NOK");
                }
            }
        }

        function execute()
        {
            unitTest();
        }

    </script>
</body>
</html>
